﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverApp.ViewModel
{
    public class AccountViewModel : ViewModelBase
    {
        private LiveService.User            upUser;
        private String                      email2;
        private String                      passw2;
        private ICommand                    updateCommand;
        private HomeViewModel               mparent;

        public AccountViewModel(HomeViewModel parent)
        {
            mparent = parent;
            this.upUser = mparent.CurrentUser;
            updateCommand = new RelayCommand(UpdateUser, CanUpdateUser);
        }

        public LiveService.User UpUser
        {
            get { return this.upUser; }
            set { this.upUser = value; }
        }

        public String EmailConfirm
        {
            get { return email2; }
            set { this.email2 = value; }
        }

        public String PasswordConfirm
        {
            get { return passw2; }
            set { this.passw2 = value; }
        }

        public String Firstname
        {
            get { return this.UpUser.firstname; }
            set { this.UpUser.firstname = value; }
        }

        public String Lastname
        {
            get { return this.UpUser.lastname; }
            set { this.UpUser.lastname = value; }
        }

        public String Email
        {
            get { return this.UpUser.email; }
            set { this.UpUser.email = value; }
        }

        public String Login
        {
            get { return this.UpUser.login; }
            set { this.UpUser.login = value; }
        }

        public String Password
        {
            get { return this.UpUser.password; }
            set { this.UpUser.password = value; }
        }

        public ICommand UpdateCommand
        {
            get { return this.updateCommand; }
            set { this.updateCommand = value; }
        }

        public void UpdateUser(object parameter)
        {
           if (this.CheckForm() == true)
            {
                try
                {
                    this.Sclient.updateUserCompleted += new EventHandler<LiveService.updateUserCompletedEventArgs>(UpdateUserCompleted);
                    this.Sclient.updateUserAsync(this.upUser);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public void UpdateUserCompleted(object sender, LiveService.updateUserCompletedEventArgs e)
        {
            try
            {
                this.mparent.AccountWin.Close();
                this.Sclient.getUserProfileByLoginCompleted += new EventHandler<LiveService.getUserProfileByLoginCompletedEventArgs>(LoadUserCompleted);
                this.Sclient.getUserProfileByLoginAsync(this.upUser.login);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void LoadUserCompleted(object sender, LiveService.getUserProfileByLoginCompletedEventArgs e)
        {
            try
            {
                this.upUser = e.Result;
                this.mparent.RegisterUser(this.upUser);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public bool CheckForm()
        {
            bool result = true;

            if (this.PasswordConfirm.Trim() != this.Password.Trim())
            {
                result = false;
                //this.mparent. = "Error in Password : " + this.PasswordConfirm + " != " + this.Password;
            }
            else if (this.Email.Trim() != this.EmailConfirm.Trim())
            {
                result = false;
                //this.mparent.Message = "Error in email";
            }
            else
            {
                result = true;
            }
            return result;
        }

        public bool CanUpdateUser(object parameter)
        {
            return true;
        }
    }
}
